import time
from selenium.webdriver.common.by import By


class  ManagementOperation:

###################################     理财操作前后     ###################################
    def management_before(self,driver,username,password,case):
        global flag
        flag = 0
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[ @ id = 'leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(2)
        try:
            # 获取第一张银行卡的金额
            before = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
            time.sleep(1)
            try:
                # 点击设置第一张卡位默认
                driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[3]").click()
                time.sleep(1)
            except:
                time.sleep(1)
            if case == "多个银行卡-默认支付测试-其他":
                # 点击设置第二张卡位默认
                driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]").click()
                flag = 1
                # 获取第二张银行卡的金额
                before = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[2]").text
                time.sleep(1)
        except:
            before = None
        # 点击个人理财
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        time.sleep(1)
        return flag,before

    def management_after(self,driver,payment_password,flag,before,money):
        # 输入支付密码
        driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/input").send_keys(payment_password)
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[3]/div[3]/a[1]").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[ @ id = 'leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(2)
        try:
            # 获取第一张银行卡的金额
            after = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
            if flag != 0:
                # 获取第二张银行卡的金额
                time.sleep(1)
                after = driver.find_element(By.XPATH,
                                            "/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[2]").text
            after = float(after[5:-1])
            before = float(before[5:-1]) - float(money[:-1])
        except:
            after = False
        return before,after
###################################      零钱理财       ###################################
    def small_change(self,driver):
        # 点击零钱理财
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[1]/a").click()
        time.sleep(1)
        # 获取支付宝零钱理财金额
        money = driver.find_element(By.XPATH,
                                    "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]").text  # .text取出文本数据
        time.sleep(1)
        # 点击投资支付宝零钱理财
        driver.find_element(By.XPATH,
                            "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button").click()
        return money
###################################      期限理财       ###################################
    def term(self,driver):
        # 点击期限理财
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[2]/a").click()
        time.sleep(1)
        # 获取支付宝期限理财金额
        money = driver.find_element(By.XPATH,
                                    "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]").text  # .text取出文本数据
        time.sleep(1)
        # 点击投资支付宝期限理财
        driver.find_element(By.XPATH,
                            "//html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button").click()
        return money
###################################      基金理财       ###################################
    def fund(self,driver):
        # 点击基金理财
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[3]/a").click()
        time.sleep(1)
        # 获取广发多元新兴股票金额
        money = driver.find_element(By.XPATH,
                                    "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]").text  # .text取出文本数据
        time.sleep(1)
        # 点击投资广发多元新兴股票
        driver.find_element(By.XPATH,
                            "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button").click()
        return money

###################################    获取实际结果     ###################################
    def getResult(self,before,after):
        if before == after:
            result = "成功"
        else:
            result = "失败"
        return result
